package com.jellyfishumbrella.util;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

public class Trace {
	public static final boolean	DEBUG		= true;
	final static Charset		ENCODING	= StandardCharsets.UTF_8;
	public static final Path	logFilePath	= Paths.get("log.txt");
	public static boolean		firstRun	= true;

	public static void getCurrentMethodName() {
		StackTraceElement stackTraceElements[] = (new Throwable()).getStackTrace();
		if (DEBUG) {
			System.out.println(stackTraceElements[1].toString());
		}
	}

	public static void getCurrentMethodName(String str) {
		StackTraceElement stackTraceElements[] = (new Throwable()).getStackTrace();
		if (DEBUG) {
			System.out.println(stackTraceElements[1].toString());
			System.out.println("         " + str);
		}
	}

	public static void println(String str) {
		if (DEBUG) {
			System.out.println(str);
		}
	}

	public static void log(String str) {
		ArrayList<String> aLines = new ArrayList();
		aLines.add(str);
		log(aLines);
	}

	public static void log(List<String> aLines) {
		try {
			if (firstRun) {
				Files.write(logFilePath, aLines, ENCODING);
			} else {
				Files.write(logFilePath, aLines, ENCODING, StandardOpenOption.APPEND);
			}
			firstRun = false;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
